package oyl.selenium.config;

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.ArrayList;

/**
 * Created by Nicky.Tang on 2021/9/18 2:15 下午
 *
 * @since 02.12.10
 */
public class AbstractDriver {
    public static String lastWindowHandle = null;

    public WebDriver driver;

    public JavascriptExecutor executorDriver;

    public AbstractDriver(WebDriver driver) {
        this.driver = driver;
        this.executorDriver = (JavascriptExecutor) driver;
    }

    public interface IDoSyncRes {
        boolean doSync();
    }
    public interface IDoSync {
        void doSync();
    }

    public boolean tryDo(int tryTimes, AbstractDriver.IDoSyncRes iDoSync) {
        int i = 0;
        while (i < tryTimes) {
            try {
                if(iDoSync.doSync()){
                    return true;
                }
            } catch (Exception ex) {
                System.out.println("翻译，没找到元素YPqjbf。。。");
            }
            this.sleep(200);
            i++;
        }
        return false;
    }

    public void doSync(String stepDesc, By var1, IDoSync iDoSync) {
        iDoSync.doSync();
        waitSubmit(var1);
        System.out.println(">>>>>>" + stepDesc + "成功...");
    }

    public void doSync(String stepDesc, String nextPageOneId, IDoSync iDoSync) {
        iDoSync.doSync();
        waitSubmit(nextPageOneId);
        System.out.println(">>>>>>" + stepDesc + "成功...");
    }

    public void doSync(String nextPageOneId, IDoSync iDoSync) {
        iDoSync.doSync();
        waitSubmit(nextPageOneId);
    }

    public void waitSubmit(By var1) {
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                WebElement webElement = null;
                try {
                    webElement = d.findElement(var1);
                } catch (NoSuchElementException ex) {
                    ex.printStackTrace();
                }
                return webElement != null;
            }
        });
    }

    public void waitSubmit(String nextPageOneId) {
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                WebElement webElement = null;
                try {
                    webElement = d.findElement(By.id(nextPageOneId));
                } catch (NoSuchElementException ex) {
                    System.out.println("未找到组件：" + nextPageOneId);
                    //ex.printStackTrace();
                }
                return webElement != null;
            }
        });
    }

    public void sleepMoment() {
        this.sleep(200);
    }

    public void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void sleepStatic(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void open(String stepDesc, By var1, IDoSync iDoSync) {
        doOpen(iDoSync);
        waitSubmit(var1);
        System.out.println(">>>>>>" + stepDesc + "成功...");
    }

    public void open(String stepDesc, String nextPageOneId, IDoSync iDoSync) {
        doOpen(iDoSync);
        waitSubmit(nextPageOneId);
        System.out.println(">>>>>>" + stepDesc + "成功...");

    }

    private void doOpen(IDoSync iDoSync) {
        lastWindowHandle = this.driver.getWindowHandle();
        iDoSync.doSync();
        this.sleep(1000);

        ArrayList<String> tabs = new ArrayList<String>(this.driver.getWindowHandles());
        this.driver.switchTo().window(tabs.get(tabs.size() - 1));//必须手动切换一下

        this.driver.navigate().refresh();//不刷新界面，总是卡着
        this.sleep(2000);
    }

    public void switchTab(String targetWindowHandle) {
        ArrayList<String> tabs = new ArrayList<String>(this.driver.getWindowHandles());
        for (int i = 0; i < tabs.size(); i++) {
            if (tabs.get(i).equals(targetWindowHandle)) {
                lastWindowHandle = this.driver.getWindowHandle();
                this.driver.switchTo().window(tabs.get(i));
                break;
            }
        }
        this.sleepMoment();
        this.driver.navigate().refresh();//不刷新界面，总是卡着
        this.sleep(2000);
    }

    public void switchToLastTab() {
        this.switchTab(lastWindowHandle);
    }
}
